/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;


import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * trida pro zjisteni pocasi, naplni tridu YRWeatherForecast daty pomoci YRXmlHandleru,
 * v metode getWeather,vraci predpovedi pocasi v urcitych formatech(napr. predpoved pro
 * dva dny atd.)
 * @author stana
 */
public class YRWeather
{
    /**
     * vrati predpoved pro urcite misto
     * @param location
     * @return predpoved
     */
//    public String getWeather(Location location)
//    {
//
//         YRWeatherForecast forecast=null;
//         String url="http://api.yr.no/weatherapi/locationforecast/1.8/?lat="+
//                 location.getLatitude()+";lon="+location.getLongtitude();
//
//        try
//        {
//            XMLReader parser = XMLReaderFactory.createXMLReader();
//            //InputSource is=new InputSource("/home/stana/Desktop/yr.xml");
//            InputSource is=new InputSource(new InputStreamReader(new URL(url).openStream()));
//
//            YRXmlHandler handler=new YRXmlHandler();
//            parser.setContentHandler(handler);
//            parser.parse(is);
//            forecast=handler.getForecast();
//            //System.out.println(forecast.toString());
//        }
//        catch (Exception ex)
//        {
//            Logger.getLogger(YRWeather.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return forecast.twoDayForecast();
//    }



    public String getWeather(double latitude,double longtitude)
    {

         YRWeatherForecast forecast=null;
         String url="http://api.yr.no/weatherapi/locationforecast/1.8/?lat="+
                 latitude+";lon="+longtitude;

        try
        {
            XMLReader parser = XMLReaderFactory.createXMLReader();
            //InputSource is=new InputSource("/home/stana/Desktop/yr.xml");
            InputSource is=new InputSource(new InputStreamReader(new URL(url).openStream()));

            YRXmlHandler handler=new YRXmlHandler();
            parser.setContentHandler(handler);
            parser.parse(is);
            forecast=handler.getForecast();
            //System.out.println(forecast.toString());
        }
        catch (Exception ex)
        {
            Logger.getLogger(YRWeather.class.getName()).log(Level.SEVERE, null, ex);
        }
        return forecast.twoDayForecast();
    }
}